package com.example.forum.controller;

import com.example.forum.commen.AppResult;
import com.example.forum.commen.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;

    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                          @RequestParam("content") @NonNull String content) {

        //获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        //1.当前登录用户的状态，禁言不能发帖
        if(user.getState() == 1) {
            //用户禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //2.不能给自己发私信
        if(user.getId() == receiveUserId) {
            return AppResult.failed("不能给自己发送站内信");
        }
        //3.校验接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() == 1) {
            //接收者状态异常
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        //4.封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());//发送者id
        message.setReceiveUserId(receiveUserId);//接收者id
        message.setContent(content);//站内信内容
        //5.调用service
        messageService.create(message);
        //6.返回结果
        return AppResult.success("发送成功");
    }

    /*未读站内信*/
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount (HttpServletRequest request) {
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        Integer count = messageService.selectUnreadCount(user.getId());
        //返回结果
        return AppResult.success(count);
    }

    /*获取站内信列表*/
    @GetMapping("getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request) {
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        //返回结果
        return AppResult.success(messages);
    }

    /*站内信状态更新为已读*/
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @RequestParam("id") @NonNull Long id) {
        //根据id查看站内信
        Message message = messageService.selectById(id);
        //站内信是否存在
        if(message == null || message.getDeleteState() == 1) {
            ///返回错误信息
            return AppResult.failed(ResultCode.FAILED_MESSAGE_EXISTS);
        }
        //站内信是否是自己的
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId() != message.getReceiveUserId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //调用service
        messageService.updateStateById(id,(byte) 1);
        return AppResult.success();
    }


    /*回复帖子*/
    @PostMapping("/reply")
    public  AppResult reply(HttpServletRequest request,
                            @RequestParam("repliedId") @NonNull Long repliedId,
                            @RequestParam("content") @NonNull String content) {
        //校验登录用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验要恢复站内信状态
        Message existsMessage = messageService.selectById(repliedId);
        if(existsMessage == null || existsMessage.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_MESSAGE_EXISTS);
        }
        //不能给自己回复
        if(user.getId() == existsMessage.getPostUserId()) {
            //返回错误信息
            return AppResult.failed("不能给自己回复");
        }
        //构建Message对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existsMessage.getPostUserId());
        message.setContent(content);
        //调用service
        messageService.reply(repliedId,message);
        //返回结果
        return AppResult.success();
    }
}
